home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Dynamically allocating memory for a char*
- Date: 19 Jan 1996 20:50:29 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan19155029@g7240065.bridge.bst.bls.com>
- References: <4dmn1i$10t@walrus2.walrus.com>
- <NITIN.96Jan19095012@more.eng.sun.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: nitin@more.eng.sun.com's message of 19 Jan 1996 17:50:12 GMT
-
- In article <NITIN.96Jan19095012@more.eng.sun.com> nitin@more.eng.sun.com (Nitin More [CONTRACTOR]) writes:
-
- : [original attribution not included in response]
- : > I have a question about allocating memory for a string as it's passed
- : > in by the user. I'm using cin to get the info, but maybe this is
- : > inappropriate. I have included the relevant snippets of the file
- : > below. What I am trying to achieve is no limitation for the user when
- : > they input a path + filename. Is it possible to use char* foo and
- : > then something like (I know this is wrong, but the idea of it ...)
-
- : To read a string from cin, you need a place to store it (char *) with enough
- : memory allocated. Until you read the string, you won't know what is enough.
- : This is a catch-22. The best you can do is use a temporary variable with
- : (reasonable) maximum possible size, read the string in the temporary variable,
- : get the size plus one for the NULL character, allocate that much memory
- : pointed to by your original variable and copy the string into it from the
- : temporary variable.
-
- : Since you may get into situation where the input string is bigger than the
- : maximum size you have chosen, you could do the following. Set the last char
- : in the temporary variable to be NULL (i.e. 0) and check if it is still the
- : same after reading the string. If it is same then you are safe. If not, the
- : string read is bigger than your maximum size and you don't have the complete
- : string. [Worse yet, you might have overwritten memory of some other
- : variable]. Either you can display an error and quit or reallocate bigger size
- : (e.g., 2*maximum) for the temporary and try again. Try until you succeed.
-
- : Anybody else knows a better way?
-
- Yes.
- You could use a string class, which already deals with dynamic allocation of
- strings. As most have stream operators defined on them, they work exactly
- as desired.
-
- or you could write the class yourself
-
- or use a strstream:
-
- char ch;
- ostrstream ostr;
-
- // If strings are defined as white space delimited then you probably
- // want to gobble up any leading white space before entering the
- // reading loop. Maybe you should check cin.flags() & ios::skipws != 0
- while (cin >> ch) {
- if (isspace(ch)) {
- cin.putback(ch);
- break;
- }
- ostr << ch;
- }
-
- ostr << ends;
- char* filename = ostr.str();
-
- ... do stuff ...
-
- delete[] filename; // Don't forget to clean up your responsibilites.
-
- or if you can guarentee the filename will end at eof ('; which is rarely
- the case ;') then more simply:
-
- ostrstream ostr;
- cin >> ostr.rdbuf();
- ostr << ends;
- char* filename = ostr.str();
-
- ... do stuff ...
-
- delete[] filename; // Don't forget to clean up your responsibilites.
-
- Regards
-
- -A.
- --
- | A.Champion |
-